$(function () {
    $(".upf").click(function () {
        $("#file").click();
    })
    $("#jqGrid").jqGrid({
        url: baseURL + 'qyxxddddyw/list',
        datatype: "json",
        colModel: [
            { label: 'id', name: 'id', index: "id", width: 45, key: true,hidden:true },
            { label: '查看详情', name: 'ddxq', index: 'XQ', width: 40,
                formatter:function (cellvale, options, rowObject){
                    return "<a class='btn btn-modify'  onclick='vm.detail(\"" + rowObject.id + "\", \"" + rowObject.deptId + "\")' style='display: inline;'>详情</a>";
                }
            },
			{ label: '企业名称', name: 'ddywQymc', index: 'DDYW_QYMC', width: 80 }, 
			{ label: '企业代码', name: 'ddywQydm', index: 'DDYW_QYDM', width: 80 }, 
			{ label: '登记序号', name: 'ddywDjxh', index: 'DDYW_DJXH', width: 80 ,hidden:true },
			{ label: '典当人姓名', name: 'ddywDdrxm', index: 'DDYW_DDRXM', width: 80 }, 
			{ label: '典当人证件号码', name: 'ddywDdrzjhm', index: 'DDYW_DDRZJHM', width: 80,hidden:true  },
			{ label: '典当人联系电话', name: 'ddywDdrlxdh', index: 'DDYW_DDRLXDH', width: 80,hidden:true  },
			{ label: '经办人姓名', name: 'ddywJbrName', index: 'DDYW_JBR_NAME', width: 80 ,hidden:true },
			{ label: '经办人编码', name: 'ddywJbrCode', index: 'DDYW_JBR_CODE', width: 80 ,hidden:true },
			{ label: '业务类型名称', name: 'ddywYwlxName', index: 'DDYW_YWLX_NAME', width: 80,hidden:true  },
			{ label: '业务类型代码', name: 'ddywYwlxCode', index: 'DDYW_YWLX_CODE', width: 80 ,hidden:true },
			{ label: '名称', name: 'ddywMc', index: 'DDYW_MC', width: 80  },
			{ label: '规格', name: 'ddywGg', index: 'DDYW_GG', width: 80 ,hidden:true },
			{ label: '数量', name: 'ddywSl', index: 'DDYW_SL', width: 80,hidden:true  },
			{ label: '估价', name: 'ddywGj', index: 'DDYW_GJ', width: 80 ,hidden:true },
			{ label: '典当金额', name: 'ddywDdje', index: 'DDYW_DDJE', width: 80 }, 
			{ label: '照片1', name: 'ddywImg1', index: 'DDYW_IMG1', width: 80 ,hidden:true},
			{ label: '照片2', name: 'ddywImg2', index: 'DDYW_IMG2', width: 80 ,hidden:true},
			{ label: '照片3', name: 'ddywImg3', index: 'DDYW_IMG3', width: 80,hidden:true },
			{ label: '备注', name: 'ddywBz', index: 'DDYW_BZ', width: 80 ,hidden:true },
			{ label: '删除状态', name: 'deleted', index: 'DELETED', width: 80 ,hidden:true },
			{ label: '登记日期代码', name: 'ddywDjrqCode', index: 'DDYW_DJRQ_CODE', width: 80 ,hidden:true },
			{ label: '登记日期', name: 'ddywDjrqName', index: 'DDYW_DJRQ_NAME', width: 80 }
        ],
		viewrecords: true,
        height: 365,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
        q:{

        },
		showList: true,
        showDetail: false,
		title: null,
		qyxxDdDdyw: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.qyxxDdDdyw = {};
            $(".upimg").html("");
		},
		update: function (event) {
            var id = getSelectedRow();
            if(id == null){
                return ;
            }
			vm.showList = false;
            vm.title = "修改";
            vm.getInfo(id);

		},

		saveOrUpdate: function () {

            if ( vm.qyxxDdDdyw.ddywDjxh ==null) {
                alert("登记序号不能为空！");
                return false;
            }
            if ( vm.qyxxDdDdyw.ddywDdrxm ==null) {
                alert("典当人姓名不能为空！");
                return false;
            }
            if ( vm.qyxxDdDdyw.ddywMc ==null) {
                alert("典当名称不能为空！");
                return false;
            }
            var nianf="";
            $(".upimg  .ddimg img").each(function(){
                nianf+=$(this).attr('src')+","
            });
            nianf=nianf.substring(0,nianf.length-1);
            $("#furl1").val(nianf);
			var url = vm.qyxxDdDdyw.id == null ? "qyxxddddyw/save" : "qyxxddddyw/update";
            vm.qyxxDdDdyw.ddywImg1 = $("#furl1").val();
            vm.qyxxDdDdyw.ddywDjrqName=$("#djDate").val();


			$.ajax({
				type: "POST",
			    url: baseURL + url,
			    contentType: "application/json",
                async: false,
			    data: JSON.stringify(vm.qyxxDdDdyw),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "qyxxddddyw/delete",
				    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get(baseURL + "qyxxddddyw/info/"+id, function(r){
                vm.qyxxDdDdyw = r.qyxxDdDdyw;

                if(vm.qyxxDdDdyw.ddywImg1){
                    var str=vm.qyxxDdDdyw.ddywImg1;
                    var texts='';
                    var imgs='';
                    var strs= new Array(); //定义一数组
                    var strs=str.split(","); //字符分割
                    for (var i=0;i<strs.length ;i++ ){
                        texts+="<div class='ddimg'><img src='"+strs[i]+"'/></div>";
                        imgs+="<div class='ddimg'><img src='"+strs[i]+"'/><div class='uclose fa fa-times-circle' onclick=''></div></div>";
                    }
                    $(".xsimg").html(texts);
                    $(".upimg").html(imgs);
                    $(".uclose").on("click",function(){
                        $(this).parent().remove();
                    });
                }
            });
		},
        detail: function (id) {
            vm.showDetail = true;
            vm.getInfo(id);
        },

        reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page,
                postData:{'name':vm.q.name}
            }).trigger("reloadGrid");
		}
	}
});


function fileupload(thisid){
    if(!(/(?:jpg|gif|png|jpeg)$/i.test(thisid.value.toLowerCase()))) {
         alert("只允许上传jpg、gif、png、jpeg格式的文件，请重新选择文件");
        return false;
    }
    var len=$(".ddimg").length;
    if(len==3){
        alert("最多上传3张图片");
        return false;
    }
    var fd=new FormData();
    fd.append('file',$("#file").get(0).files[0]);
    $.ajax({
        type: "post",
        dataType: "json",
        async: false,
        cache: false,
        url: '/fileTransfer/upload',
        processData: false,  // 告诉jQuery不要去处理发送的数据
        contentType: false,   // 告诉jQuery不要去设置Content-Type请求头
        data: fd,
        //}).then(function (result) {
        success: function (result) {
            if (result.code == 0) {
                var qq=JSON.stringify(result);
                console.log(result.path[0]);
                $("#furl1").val(result.path[0]);
                $(".upimg").append("<div class='ddimg'><img src='"+result.path[0]+"'/><div class='uclose fa fa-times-circle' onclick=''></div></div>");
                $(".uclose").on("click",function(){
                     $(this).parent().remove();
                });
            }
        },

    });

}
function crem(){

    //$(this).parent("div").empty();
}